<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\table\util\tfilter;

/**
 * 下拉选择控件
 * @author cleverstone
 * @since ym2.0
 */
class Select implements FilterControlInterface
{
    /**
     * @var array 选项
     */
    private $_options = [
        'control' => 'select',
        'label' => '',
        'placeholder' => '请选择',
        'default' => '',
        'class' => '',
        'style' => '',
        'attribute' => '',
        'options' => [],
        'multiple' => false, // 是否多选
        'disabled' => [],//禁用项
        'uiConfig' => [
            'allowClear' => true,//允许清除选中
            'width' => '100%',//控件宽
            'selectedInOrder' => true, //多选时,是否按选择顺序排序
            'selectedHideOption' => true, //多选时,是否隐藏已选择过的选项
            'closeOnSelect' => true, //选择后,是否关闭选项内容
        ],
    ];

    /**
     * 选择后,不关闭选项内容
     * @return $this
     */
    public function unCloseOnSelect()
    {
        $this->_options['uiConfig']['closeOnSelect'] = false;
        return $this;
    }

    /**
     * select2 配置
     * @param string $key 配置
     * @param mixed $value 值
     * @return $this
     */
    public function uiConfig($key, $value)
    {
        $this->_options['uiConfig'][$key] = $value;
        return $this;
    }

    /**
     * 设置禁用项
     * @param array $item
     * @return $this
     */
    public function disabled(array $item)
    {
        $this->_options['disabled'] = $item;
        return $this;
    }

    /**
     * 多选
     * @return $this
     */
    public function multiple()
    {
        $this->_options['multiple'] = true;
        return $this;
    }

    /**
     * @param string $label 标签
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = (string)$label;
        return $this;
    }

    /**
     * @param string $placeholder 内容提示
     * @return $this
     */
    public function placeholder($placeholder)
    {
        $this->_options['placeholder'] = (string)$placeholder;
        return $this;
    }

    /**
     * 默认值
     * @param string $default
     * @return $this
     */
    public function defaultValue($default = '')
    {
        $this->_options['default'] = (string)$default;
        return $this;
    }

    /**
     * @param string|array $class CSS类
     * @return $this
     */
    public function uiClass($class)
    {
        $this->_options['class'] = $class;
        return $this;
    }

    /**
     * @param string|array $style CSS样式
     * @return $this
     */
    public function style($style)
    {
        $this->_options['style'] = $style;
        return $this;
    }

    /**
     * @param array|string $attribute HTML标签属性
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->_options['attribute'] = $attribute;
        return $this;
    }

    /**
     * @param array $options 下拉选项
     * @return $this
     */
    public function options(array $options)
    {
        $this->_options['options'] = $options;
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        // 多选默认值
        if ($this->_options['multiple'] && !in_array(substr(trim($this->_options['default']), 0, 1), ['[', '{'])) {
            $this->_options['default'] = '[]';
        }

        return $this->_options;
    }
}